﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace MNNUTEM
{
    public partial class Sucursales : Form
    {
       
        private crudSucursal cSucursal;
        int ID;

        public Sucursales()
        {
            InitializeComponent();
            cSucursal = new crudSucursal();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
           cSucursal.Insert(textBox2,textBox6,textBox1,textBox4,textBox7);
           updateGridView();
           clean();

          
            
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            cSucursal.Delete(ID);
            updateId(ID);
            updateGridView();
        }

        private void updateGridView()
        {
            dataGridView1.DataSource = cSucursal.Consultation("select idSucursal,fonoSucursal,direccionSucursal,cuidadSucursal,arriendoSucursal from  Sucursal ");
        
        }

        private void Sucursales_Load(object sender, EventArgs e)
        {
            updateGridView();
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
          dataGridView1.Columns[0].HeaderText = "Id Sucursal";
          dataGridView1.Columns[1].HeaderText = "Fono";
          dataGridView1.Columns[2].HeaderText = "Dirección";
          
          dataGridView1.Columns[3].HeaderText = "Cuidad";
        
          dataGridView1.Columns[4].HeaderText = "Arriendo";

          dataGridView1.AutoResizeColumns(); 
            dataGridView1.Columns[2].Width = 270; dataGridView1.Columns[1].Width = 100; dataGridView1.Columns[4].Width = 100;
          
            
        }

        private void getdatos()
        {
            DataSet DS;
            DS = cSucursal.Consultation2("select *from Sucursal where idSucursal='" + ID + "'");
            textBox2.Text = DS.Tables[0].Rows[0][0].ToString();
            textBox1.Text = DS.Tables[0].Rows[0][2].ToString();
         //   textBox3.Text = DS.Tables[0].Rows[0][3].ToString();
            textBox4.Text = DS.Tables[0].Rows[0][4].ToString();
       //     textBox5.Text = DS.Tables[0].Rows[0][5].ToString();
            textBox6.Text = DS.Tables[0].Rows[0][1].ToString();
            textBox7.Text = DS.Tables[0].Rows[0][6].ToString();

        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            ID= int.Parse(dataGridView1[0, e.RowIndex].Value.ToString());
            getdatos();
            button3.Enabled = true;
            button4.Enabled = true;
            button1.Enabled = false;
            button2.Enabled = true;
            dataGridView2.DataSource = cSucursal.Consultation("select idProducto,nombreProducto,cantidadProducto,caducaProducto,Demanda from Producto where Sucursal_idSucursal='"+ID+"' ");
            dataGridView2.Columns[1].Width = 370;
            dataGridView2.Columns[0].HeaderText = "Id Producto";
            dataGridView2.Columns[1].HeaderText = "nombre Producto";
        }

        private void updateId(int idProducto)
        {

            DataSet DS;
            DS = cSucursal.Consultation2("select count(idSucursal) from Sucursal");
            int count = int.Parse(DS.Tables[0].Rows[0][0].ToString()) + 1;

            if (idProducto != count)
                for (int i = idProducto; i <= count; i++)
                {
                    
                    cSucursal.SetId(i, i + 1);
                }
            else { }

         


        }


        private void button4_Click(object sender, EventArgs e)
        {
            cSucursal.Set(textBox2,textBox6, textBox1, textBox4, textBox7, ID);
            updateGridView();
        }

        private void clean()
        {

            textBox1.Text = "";
            textBox2.Text = "";
           
            textBox4.Text = "";
            
            textBox6.Text = "";
            textBox7.Text = "";
        
        }

        private void button2_Click(object sender, EventArgs e)
        {
            clean();
            button3.Enabled = false;
            button4.Enabled = false;
            button1.Enabled = true;
            button2.Enabled = true;
        }
    }
}
